﻿using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using Zeta.Bot;

namespace LocationTracker
{

    class TabUI
    {
        private static Button btnDumptoConsole, btnDumptoFiles;

        internal static void InstallTab()
        {
            Application.Current.Dispatcher.Invoke(

                new System.Action(
                    () =>
                    {
                        btnDumptoConsole = new Button
                        {
                            Width = 120,
                            HorizontalAlignment = HorizontalAlignment.Left,
                            VerticalAlignment = VerticalAlignment.Top,
                            Margin = new Thickness(3),
                            Content = "Dump to Console"
                        };

                        btnDumptoFiles = new Button
                        {
                            Width = 120,
                            HorizontalAlignment = HorizontalAlignment.Left,
                            VerticalAlignment = VerticalAlignment.Top,
                            Margin = new Thickness(3),
                            Content = "Dump to Files"
                        };

                        Window mainWindow = Application.Current.MainWindow;

                        btnDumptoConsole.Click += new RoutedEventHandler(btnDumptoConsole_Click);
                        btnDumptoFiles.Click += new RoutedEventHandler(btnDumptoFiles_Click);

                        UniformGrid uniformGrid = new UniformGrid()
                        {
                            HorizontalAlignment = HorizontalAlignment.Stretch,
                            VerticalAlignment = VerticalAlignment.Top,
                            MaxHeight = 180
                        };


                        uniformGrid.Children.Add(btnDumptoConsole);
                        uniformGrid.Children.Add(btnDumptoFiles);

                        tabItem = new TabItem()
                        {
                            Header = "Location Tracker",
                            ToolTip = "Bounty Development Tools",
                        };

                        tabItem.Content = uniformGrid;

                        var tabs = mainWindow.FindName("tabControlMain") as TabControl;
                        if (tabs == null)
                            return;

                        tabs.Items.Add(tabItem);
                    }
                )
            );
        }

        private static TabItem tabItem;

        internal static void RemoveTab()
        {
            Application.Current.Dispatcher.Invoke(
                new System.Action(
                    () =>
                    {
                        Window mainWindow = Application.Current.MainWindow;
                        var tabs = mainWindow.FindName("tabControlMain") as TabControl;
                        if (tabs == null)
                            return;
                        tabs.Items.Remove(tabItem);

                    }
                )
            );
        }

        private static void btnDumptoFiles_Click(object sender, RoutedEventArgs e)
        {
            Cache.DumpToFiles();
        }

        private static void btnDumptoConsole_Click(object sender, RoutedEventArgs e)
        {
            Cache.DumpToConsole();
        }

    }

}
